package com.zrrd.yunchmall.order.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrrd.yunchmall.order.entity.OrderReturnApply;
import com.zrrd.yunchmall.order.service.IOrderReturnApplyService;
import com.zrrd.yunchmall.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.time.LocalDateTime;

/**
 * <p>
 * 订单退货申请 前端控制器
 * </p>
 *
 * @author LYY
 * @since 2024-01-15
 */
@RestController
@RequestMapping("/returnApply")
@Api("退货处理")
@SuppressWarnings("all")
public class OrderReturnApplyController {

    @Autowired
    private IOrderReturnApplyService orderReturnApplyService;

    @GetMapping("/{id}")
    @ApiOperation("处理详情")
    private ResponseResult select(@PathVariable("id") Integer id){
        OrderReturnApply orderReturnApply=orderReturnApplyService.getById(id);
        return new ResponseResult<>(200,"查询成功!",orderReturnApply);
    }



    @GetMapping("/list")
    @ApiOperation("处理记录列表")
    public ResponseResult list(@RequestParam int pageNum,
                               @RequestParam int pageSize,
                               Integer id,Integer status,Integer handleMan,
                               String createTime,String handleTime){
        QueryWrapper queryWrapper=new QueryWrapper<>();
        if (id!=null){
            queryWrapper.eq("id",id);
            return new ResponseResult<>(200,"查询成功",
                    orderReturnApplyService.page(new Page<>(pageNum,pageSize),queryWrapper));
        }
        else {
            if (status!=null){
                queryWrapper.eq("status",status);
            }
            if (!StringUtils.isEmpty(handleMan)){
                queryWrapper.like("handle_man",handleMan);
            }
            if (!StringUtils.isEmpty(createTime)){
                queryWrapper.between("create_time",createTime+
                        " 00:00:00",createTime+" 23:59:59");
            }
            if (!StringUtils.isEmpty(handleTime)){
                queryWrapper.between("handle_time",handleTime+
                        " 00:00:00",handleTime+" 23:59:59");
            }
            return new ResponseResult<>(200,"查询成功",
                    orderReturnApplyService.page(new Page<>(pageNum,pageSize),queryWrapper));
        }
    }
    @PostMapping("/update/status/{id}")
    @ApiOperation("退货处理")
    public ResponseResult updateStatus(@PathVariable("id") Integer id,@RequestBody OrderReturnApply orderReturnApply){
        UpdateWrapper updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("id",id);
        updateWrapper.set("status",orderReturnApply.getStatus());
        updateWrapper.set("handle_note",orderReturnApply.getHandleNote());
        updateWrapper.set("handle_time", LocalDateTime.now());
        if (orderReturnApply.getStatus()==3){//同意退货
            updateWrapper.set("company_address_id",orderReturnApply.getCompanyAddressId());
            updateWrapper.set("return_amount",orderReturnApply.getReturnAmount());
        }
        if (orderReturnApply.getStatus()==2){
            updateWrapper.set("receive_man","当前登录用户");
            updateWrapper.set("receive_time",LocalDateTime.now());
            updateWrapper.set("receive_note",orderReturnApply.getReceiveNote());
        }
        orderReturnApplyService.update(updateWrapper);
        return new ResponseResult<>(200,"退货处理");
    }
    @PostMapping("/delete")
    @ApiOperation("批量删除退货记录")
    private ResponseResult deleteByIds(@RequestParam  String ids){
        for (String id:ids.split(",")) {
            orderReturnApplyService.removeById(id);
        }
        return new ResponseResult<>(200,"删除成功!");
    }
}
